<?
include_once 'CoreModel.php';
Class OrderModel extends  CoreModel {
	private $table;
	function __construct() {
		$this -> table = "material_data";
		parent::__construct();
	}

	public function getOrderTypeID($id) {
		$this -> db -> select("*");
		$this -> db -> where("order_id", $id);
		return $this -> Execute("ORDER_TYPE", true);
	}

	public function getOrderType() {
		$this -> db -> select("*");
		return $this -> Execute("ORDER_TYPE");
	}
	public function deleteOrder($orderID){
		
		$this->db->set("OrderStatus","Delete");
		$this->db->where("OrderID",$orderID);
		return $this->db->update("tb_order");		
	}
	public function getOrderReasonID($id) {
		$this -> db -> select("*");
		$this -> db -> where("reason_id", $id);
		return $this -> Execute("ORDER_REASON", true);
	}

	public function getOrderReason() {
		$this -> db -> select("*");
		return $this -> Execute("ORDER_REASON");
	}

	public function getItemCategoryID($id) {
		$this -> db -> select("*");
		$this -> db -> where("cat_id", $id);
		return $this -> Execute("ITEM_CATEGORIES", true);
	}

	public function getItemCategory() {
		$this -> db -> select("*");
		return $this -> Execute("ITEM_CATEGORIES");
	}

	public function getPriceTypeID($id) {
		$this -> db -> select("*");
		$this -> db -> where("price_id", $id);
		
		return $this -> Execute("PRICE_TYPE", true);
	}

	public function getPriceType() {
		$this -> db -> select("*");
		$this->db->where("price_id != 'ZPR2'",null,null);
		return $this -> Execute("PRICE_TYPE");
	}

	public function getOrderNum() {
		return $this -> db -> count_all('tb_order');
	}
	public function getDINum() {
		return $this -> db -> count_all('tb_di');
	}
	

	public function createOrder($order) {
		$this -> db -> insert("tb_order", $order);
	}
	public function getOrder($orderID) {
		$this -> db -> select("*");
		$this -> db -> where("OrderID", $orderID);
		$data = $this -> Execute("tb_order", true);
		if($data!=false){
			$this -> db -> select("*");
			$this -> db -> where("OrderID", $orderID);
			$data->orderDetail = $this -> Execute("order_detail"); 
		}
		return $data;
	}
	public function getDI($dino) {
		$this -> db -> select("*");
		$this -> db -> where("DINO", $dino);
		$data = $this -> Execute("tb_di", true);
		if($data!=false){
			$this -> db -> select("*");
			$this -> db -> where("DINO", $dino);
			$data->orderDetail = $this -> Execute("di_detail"); 
		}
		return $data;
	}
	public function getOrderView($orderID) {
		$this -> db -> select("*");
		$this -> db -> where("OrderID", $orderID);
		return $this -> Execute("tb_order", true);
	}
	public function getOrderHead($orderID) {
		$this -> db -> select("*");
		$this -> db -> where("OrderID", $orderID);
		$this -> db -> where("OrderStatus", "Taking");
		return $this -> Execute("tb_order", true);
	}
	public function getDIHeadList($orderID) {
		$this -> db -> select("*");
		$this -> db -> where("RefNo", $orderID);
		$this->db->order_by("ShipDate","ASC");
		return $this -> Execute("tb_di");
	}
	public function getOrderOfCustomer($custID,$start,$limit) {
		//$custID = ltrim($custID,'0');
		$this -> db -> select("*");
		$this -> db -> join("zsd_web_order","tb_order.CustID = zsd_web_order.CUSTOMER_NO");
		$this -> db -> where("CustID", $custID);
		$this -> db -> where("OrderStatus", "Taking");
		$this -> db -> group_by("OrderID");
		$this -> db -> limit($limit, $start);
		return $this -> Execute("tb_order");
	}
	public function getOrderOfCustomerCount($custID) {
		//$custID = ltrim($custID,'0');
		$this -> db -> select("CustID");
		$this -> db -> where("CustID", $custID);
		$this -> db -> where("OrderStatus", "Taking");
		
		$data  = $this -> Execute("tb_order");
		return count($data);
	}
	public function insertOrder($data, $orderHead,$order,$DIDetail) {
		$orderID = $data[0]['OrderID'];
		
		
		
		
		
		
		$this -> db -> where("ORDER_NO", $orderID);
		$this -> db -> delete("zsd_web_order");
		
		$this -> db -> where("OrderID", $orderID);
		$this -> db -> delete("order_detail");
		
		$this -> db -> insert_batch("order_detail", $data);

		$this -> db -> where("OrderID", $orderID);
		$this -> db -> update("tb_order", $orderHead);
		
		$di['DINO'] = $DIDetail[0]['DINo'];
		$di['CustID'] = $order->CustID;
		$di['CustName'] = $order->CustName;
		$di['RefNo'] = $orderID;
		$di['TotalAmount'] = $orderHead['PricePaid'];
		$di['Discount'] = $orderHead["Discount"];
		$di['NetAmount'] = $di['TotalAmount'] - $di['Discount'];
		$di['CreateOn'] = date("Y-m-d H:i:s");
		$di['UpdateOn'] = date("Y-m-d H:i:s");
		$di['DIStatus'] = "Active";
		$di['SendStatus']  = "N";
		$di['ShipDate'] = $order->shipDate;
		$di['PaymentStatus'] = $orderHead['PaymentStatus'];
		$this->db->insert("tb_di",$di);
		$this->db->insert_batch("di_detail",$DIDetail);
		
		
		
		
		
		//, customer_data c
		//,c.SALES_DIST
		// and o.CustID = c.KUNNR
		
		$sql = "SELECT  o.OrderID,DocTypeID,OrderReasonID,null,CustID,seqNo,o.Discount,o.SaleID,VatType
		,d.pID,d.qty,d.SaleUnit,IsSaleProduct,PriceTypeID,d.PackRatio,d.CartonRatio,c.SALES_DIST,d.LineAmount  
 		FROM tb_order o ,order_detail d, customer_data c  where o.OrderID = d.OrderID and  o.OrderID = '$orderID' and o.CustID = c.KUNNR";
		$query = $this -> db -> query($sql);
		$data = $query -> result();
		if (!empty($data)) {
			$now = date("Ymd");
			$seq = 1;
			$allData = array();
			foreach ($data as $in => $val) {
				# Field, Type, Null, Key, Default, Extra
				$array = array();
				$array['CURRENCY'] = "THB";
				$array['CUSTOMER_NO'] = $val->CustID;
				$array['CREATE_DATE'] = $now;
				$array['ITEM_CATEGORIES'] = $val->IsSaleProduct;
				
				$array['MATERIAL_NO'] = $val->pID;
				$array['ORDER_NO'] = $orderID;
				
				$array['ORDER_REASON'] = "";
				//$array['ORDER_TYPE'] = ""; // รอ	
				$array['PRICE_TYPE'] = $val->PriceTypeID;
				$array['PRICE_UNIT'] = "THB";
				//เพิ่มฟิลด์ที่จัดเก็บ
				$array['DELIVERY_DATE'] = $now;
				$array['BILLING_DATE'] = $now;
				$array['DISCOUNT'] = $val->Discount;
				$array['SALE_EMPLOYEE'] = $val->SaleID;
				$array['PRICE_VALUE'] = $val->LineAmount;
								
				if ($val->PriceTypeID = 'ZPR1'){
					if ($val->VatType == 'VAT'){
						if ($val->SALES_DIST == 'กรุงเทพฯและปริมณฑล'){
							$array['ORDER_TYPE'] = "ZV01";
						} else {
							$array['ORDER_TYPE'] = "ZV03";
						}
					} elseif ($val->VatType == 'NOVAT') {
						if ($val->SALES_DIST == 'กรุงเทพฯและปริมณฑล'){
							$array['ORDER_TYPE'] = "ZV02";
						} else {
							$array['ORDER_TYPE'] = "ZV04";
						}
					}
				} elseif ($val->PriceTypeID == 'ZPR0') {
					if ($val->VatType == 'VAT'){
						if ($val->SALES_DIST == 'กรุงเทพฯและปริมณฑล'){
							$array['ORDER_TYPE'] = "ZC01";
						} else {
							$array['ORDER_TYPE'] = "ZC03";
						}
					} elseif ($val->VatType == 'NOVAT') {
						if ($val->SALES_DIST == 'กรุงเทพฯและปริมณฑล'){
							$array['ORDER_TYPE'] = "ZC02";
						} else {
							$array['ORDER_TYPE'] = "ZC04";
						}
					}
				} else { 
					$array['ORDER_TYPE'] = ""; 
				}			
				
				$remainQty = $val->qty;
				
			
				$carton = floor($remainQty / $val->CartonRatio);
				if($carton > 0 ){
					$array['QUANTITY'] = $carton;
					$array['UNIT'] = "CAR";
					$array['ITEM_NO'] = $seq;
					$allData[] = $array;
					$remainQty = $remainQty  - ($carton * $val->CartonRatio);
					$seq++;
				}
				
				$pack = floor($remainQty / $val->PackRatio);
				if($pack > 0 ){
					$array['QUANTITY'] = $pack;
					$array['UNIT'] = "PAC";
					$array['ITEM_NO'] = $seq;
					$allData[] = $array;
					$remainQty = $remainQty  - ($pack * $val->PackRatio);
					$seq++;
				}
				
				
				if($remainQty > 0){
					$array['QUANTITY'] = $remainQty;
					$array['UNIT'] = "EA";
					$array['ITEM_NO'] = $seq;
					$allData[] = $array;
					$seq++;
				}
			
				

			}
			$this->db->insert_batch("zsd_web_order",$allData);
			
		}
	}

}